/*
 * Copyright (c) 2010, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.helper;

import java.util.Map;

import net.miblounge.gradecalculator.core.descriptors.courses.Course;
import net.miblounge.gradecalculator.core.student.ExaminationResult;
import net.miblounge.gradecalculator.core.student.ExaminationResultType;

public final class Helper {
	
	public enum PassResult { NOTPASSED, ESTIMATED, PASSED };

	private Helper()
	{
	}

	public static String formatECTS(final float ects, final float requiredECTS)
	{
		if (ects < requiredECTS) {
			return formatECTS(ects) + " (" + formatECTS(requiredECTS) + ")";
		} else {
			return formatECTS(ects);
		}
	}
	
	public static String formatECTS(final float ects)
	{
		String result = String.valueOf(ects);
		if (result.endsWith(".0")) {
			result = result.substring(0, result.length()-2);
		}
		return result; 
	}
	
	public static PassResult getPassed(final Course c, final ExaminationResult r)
	{
		if (r == null) {
			return PassResult.NOTPASSED;
		}
		
		PassResult result = PassResult.PASSED;
		
		if (!c.getUngradedPerformance().isEmpty()){
			// ungradeded performance necessary
			// TODO: update when not every ungraded performance is estimated passed
			if (!r.getUngradedPassed()) {
				result = PassResult.ESTIMATED;
			}
		}

		if (!c.getGradedPerformance().isEmpty()){
			final String passedGrade = r.getGrade(ExaminationResultType.PASSED);
			if (passedGrade.equals("")) {
				// not passed
				final Map<ExaminationResultType, String> grades = r.getGrades();
				if (grades.size() > 0) {
					result = PassResult.ESTIMATED;
				} else {
					result = PassResult.NOTPASSED;
				}
			}
		}
		
		return result;
	}
		
}
